<template>
  <!-- 再次复用头部组件 -->
  <el-row
    type="flex"
    justify="space-between"
    style="width: 100%; height: 40px"
    align="middle"
  >
    <!-- 左侧 -->
    <el-col>
      <span>{{ treeData.name }}</span>
    </el-col>
    <!-- 右侧 -->
    <el-col :span="8">
      <el-row type="flex" justify="end">
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 下拉框组件 -->
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$emit('openDialog', treeData.id)"
                >添加子部门</el-dropdown-item
              >
              <el-dropdown-item
                v-if="!isRoot"
                @click.native="$emit('editDialog', treeData.id)"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" @click.native="delDepart"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentApi } from "@/api/departments";
export default {
  name: "TreeTools",
  props: {
    treeData: {
      type: Object,
      default: () => {},
    },
    // 是否为顶级
    isRoot: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
    },
    //删除部门
    async delDepart() {
      try {
        // 弹出确认框 发起删除请求 提示删除成功 更新列表
        await this.$confirm("确认删除吗");
        await delDepartmentApi(this.treeData.id);
        this.$message("删除成功");
        this.$emit("updateList");
      } catch (error) {
        console.log("取消成功");
      }
    },
  },
};
</script>
<style></style>
